<?php

/**
 * 
 * Enter description here ...
 * @author taras
 *
 */
class gCore extends gLinker
{	
	private $onStart 	= array();
	private $onShutdown = array();
    protected static $_loadedObjects = array();    
	
	function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $name
	 * @throws Exception
	 */
	public static function loadCoreObject($name)
	{
        return self::includeCoreObject($name);
	}

    public static function includeCoreObject($name)
    {
        if( in_array($name,self::$_loadedObjects))
        {
            return true;
        }

		$classfile = _CORE_PATH . 'classes' . DIRECTORY_SEPARATOR . $name . '.php';

		if( file_exists($classfile) )
		{
			require_once $classfile;
            self::$_loadedObjects[] = $name;
		}
		else
		{
			throw new gException('Cannot find core file');
		}
		return true;
    }
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	public function getCoreObject($name)
	{
		
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $objectname
	 * @param unknown_type $name
	 */
	public static function includePackageFile($objectname,$name)
	{
		$filename = _CORE_PATH . 'classes' . DIRECTORY_SEPARATOR . $objectname . DIRECTORY_SEPARATOR . $name . '.php';

		if( file_exists($filename))
		{
			require_once $filename;
		}
		else
		{
			throw new gException('Cannot find core object package file');
		}
		return true;
	}
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $objectName
	 */	
	public function includePackage($objectName)
	{
		
	}
	
	public static function includeAdapterFile($objectName,$name)
	{
		$filename = _CORE_PATH . 'classes' . DIRECTORY_SEPARATOR . $objectName . DIRECTORY_SEPARATOR . 'adapters' . DIRECTORY_SEPARATOR . $name . '.php';

		if( file_exists($filename))
		{
			require_once $filename;
		}
		else
		{
			throw new gException('Cannot find core object adapter file');
		}
		return true;		
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function start()
	{
		foreach( $this->onStart as $call )
		{
			call_user_func_array(array($call[0],$call[1]),$call[2]);
		}
	}
	
	public function shutdown()
	{
		foreach( $this->onShutdown as $call )
		{
			call_user_func_array(array($call[0],$call[1]),$call[2]);
		}
	}

    public static function isConsole()
    {
        return (strcmp(strtolower(substr(PHP_SAPI,0,3)),"cli")==0)?true:false;
    }
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $object
	 * @param unknown_type $method
	 * @param unknown_type $params
	 */
	public static function registerOnStart($object,$method,$params=array())
	{
		$this->onStart = array($object,$method,$params);
	}
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $object
	 * @param unknown_type $method
	 * @param unknown_type $params
	 */
	public static function registerOnShutdown($object,$method,$params)
	{
		$this->onShutdown = array($object,$method,$params);
	}
	
	public function redirect($url)
	{
		$this->shutdown();
		header('location: ' . $url);
	}
	
	public function forward($controller,$action)
	{
		
	}
	
	function __clone()
	{
		
	}
}